package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.vo.AddCartSuccessVo;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;

    /**
     *  吧上面添加到购物车
     * @return
     */
    @GetMapping("/addCart.html")
    public String addCart(@RequestParam Long skuId,
                          @RequestParam Integer skuNum,
                          Model model,
                          @RequestHeader(value = RedisConst.USER_ID_HEADER,required = false) Long userId,
                          @RequestHeader(value = RedisConst.USER_TEMP_ID_HEADER,required = false) String temId){
        //:商品添加到购物车-->远程调用购物功能吧商品添加到购物车
        Result<AddCartSuccessVo> addCartSuccessVoResult = cartFeignClient.addToCart(skuId, skuNum );

        model.addAttribute("skuInfo",addCartSuccessVoResult.getData().getSkuInfo());
        model.addAttribute("skuNum",addCartSuccessVoResult.getData().getSkuNum());

        return "cart/addCart";
    }

    /**
     * 购物车列表
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(){
        return "cart/index.html";
    }

    /**
     * 删除选中的商品
     * @return
     */
    @GetMapping("/cart/deleteChecked")
    public String deleteChecked(){
        cartFeignClient.deleteChecked();
        return "redirect:http://cart.gmall.com/cart.html";
    }
}
